const express = require('express');
const history = require('connect-history-api-fallback');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware')

const app = express();
app.use(history()); // 使用HTML5 history api

// 控制面板\系统和安全\系统\高级系统设置\环境变量 设置一个环境变量NODE_ENV
// 设置完最好重启一下命令行或开发工具
if (process.env.NODE_ENV !== 'production') { // 开发环境使用HMR
  const config = require('./config/webpack.dev.js');
  const compiler = webpack(config);
  app.use(webpackDevMiddleware(compiler, {
    logLevel: 'silent' // 隐藏控制台信息
  }));
  app.use(webpackHotMiddleware(compiler));
  app.use(express.static('dist-dev'))
} else {
  app.use(express.static('dist'))
}

// 在8080端口运行文件
app.listen(8080, function () {
  console.log('App listening on port 8080!\n');
});